﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;

namespace P3SS.ConfigUtil
{
	public partial class frmChangeStatus : Form
	{
		private ServiceAction gotoServiceAction;
		private BackgroundWorker bgwControlService;

		public frmChangeStatus(ServiceAction inServiceAction)
		{
			InitializeComponent();
			gotoServiceAction = inServiceAction;

			bgwControlService = new BackgroundWorker();
			bgwControlService.DoWork += new DoWorkEventHandler(bgwControlService_DoWork);
			bgwControlService.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwControlService_RunWorkerCompleted);

			bgwControlService.RunWorkerAsync();
		}

		void bgwControlService_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			bool success = (bool)e.Result;

			if (success)
			{
				this.DialogResult = DialogResult.Yes;
			}
			else
			{
				this.DialogResult = DialogResult.No;
			}

			this.Hide();
		}

		void bgwControlService_DoWork(object sender, DoWorkEventArgs e)
		{
			try
			{
				waitForServiceToStabilise();

				switch (gotoServiceAction)
				{
					case ServiceAction.Start:
						{
							if (srvcController.Status == ServiceControllerStatus.Stopped)
							{
								srvcController.Start();
								srvcController.WaitForStatus(ServiceControllerStatus.Running);
							}
							else if (srvcController.Status == ServiceControllerStatus.Paused)
							{
								srvcController.Continue();
								srvcController.WaitForStatus(ServiceControllerStatus.Running);
							}
							break;
						}
					case ServiceAction.Stop:
						{
							if ((srvcController.Status == ServiceControllerStatus.Running) || (srvcController.Status == ServiceControllerStatus.Paused))
							{
								srvcController.Stop();
								srvcController.WaitForStatus(ServiceControllerStatus.Stopped);
							}
							break;
						}
					case ServiceAction.Restart:
						{
							if ((srvcController.Status == ServiceControllerStatus.Running) || (srvcController.Status == ServiceControllerStatus.Paused))
							{
								srvcController.Stop();
								srvcController.WaitForStatus(ServiceControllerStatus.Stopped);
							}
							srvcController.Refresh();
							srvcController.Start();
							srvcController.WaitForStatus(ServiceControllerStatus.Running);
							break;
						}
				}

				e.Result = true;
			}
			catch
			{
			    e.Result = false;
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Hide();
		}

		private void waitForServiceToStabilise()
		{
			switch (srvcController.Status)
			{
				case ServiceControllerStatus.ContinuePending:
					srvcController.WaitForStatus(ServiceControllerStatus.Running);
					break;
				case ServiceControllerStatus.PausePending:
					srvcController.WaitForStatus(ServiceControllerStatus.Paused);
					break;
				case ServiceControllerStatus.StartPending:
					srvcController.WaitForStatus(ServiceControllerStatus.Running);
					break;
				case ServiceControllerStatus.StopPending:
					srvcController.WaitForStatus(ServiceControllerStatus.Stopped);
					break;
			}

			srvcController.Refresh();
		}

		private void frmChangeStatus_Load(object sender, EventArgs e)
		{
			try
			{
				srvcController.Refresh();
			}
			catch (Exception ex)
			{
				if (ex.InnerException is Win32Exception)
				{
					DialogResult = DialogResult.Yes;
				}
				else
				{
					DialogResult = DialogResult.No;
				}
				this.Hide();
			}
		}
	}

	public enum ServiceAction
	{
		Start,
		Stop,
		Restart,
	}
}
